package cn.iutils.sys.controller;

import java.util.List;
import java.util.Set;

import cn.iutils.common.ResultVo;
import cn.iutils.common.utils.SystemUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.iutils.common.BaseController;
import cn.iutils.sys.entity.Resource;
import cn.iutils.sys.service.ResourceService;
import cn.iutils.sys.service.UserService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 首页控制器
 * @author iutils.cn
 */
@Controller
@RequestMapping(value = "${adminPath}")
public class IndexController extends BaseController {

	@Autowired
	private ResourceService resourceService;

	@Autowired
	private UserService userService;

	/**
	 * 网页入口
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET)
	public String index(HttpServletRequest request,HttpServletResponse response,Model model) {
		//ajax登录成功处理
		if ("XMLHttpRequest".equalsIgnoreCase(request.getHeader("X-Requested-With"))) {
			ResultVo resultVo = new ResultVo(ResultVo.SUCCESS,"1","登录成功",null);
			return renderString(response,resultVo);
		}else{
			Set<String> permissions = userService.findPermissions();
			List<Resource> menus = resourceService.findMenus(permissions);
			model.addAttribute("menus", menus);
			return "index";
		}
	}

	/**
	 * 初始页面（可以自行调整）
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/welcome", method = RequestMethod.GET)
	public String welcome(Model model) {
		model.addAttribute("systemInfo", SystemUtils.getSystemInfo());
		return "welcome";
	}

}
